using System.Collections.Generic;

namespace EPiSugar.EntityAdapter
{
    public class EntityModel
    {
        public IDictionary<int, PageMapping> Mappings { get; set; }

        public EntityModel()
        {
            Mappings = new Dictionary<int, PageMapping>();
        }

        public void AddMapping(PageMapping pageTypeMapping)
        {
            if (pageTypeMapping != null)
                Mappings[pageTypeMapping.PageTypeID] = pageTypeMapping;
        }

        public PageMapping GetMapping(int pageTypeID)
        {
            if (Mappings.ContainsKey(pageTypeID))
                return Mappings[pageTypeID];
            return null;
        }

        public virtual object GetEntity(EPiServer.Core.PageData page)
        {
            PageMapping mapping = GetMapping(page.PageTypeID);
            object instance = mapping.Instantiate();
            mapping.Bind(page, instance);
            return instance;
        }
    }
}